package com.example.edu.models;

import java.util.ArrayList;
import java.util.List;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import com.example.edu.connection.connection;
import com.example.edu.models.*;
public class edu_courses {
	
	public int course_id;
	
	public String name;
		
	public String code;
	
	public int institution_id;	
	
	public int instructor_id;
	
	public long start_date;
	
	public long end_date;
	
	public String description;
	
	public String short_description;
	
	public String url;
	
	public int is_active;
	
	public int parent_id;
	
	public int created_by;
	
	public long creation_date;
	
	public List<edu_comments> comments;
	
	public List<edu_tags> tags;
	
	public static List<edu_courses> getCoursesByUserId(String user_device_id){
		
		JSONArray j=connection.getUserCourses(user_device_id);
		
		ArrayList<edu_courses> courseList=new ArrayList<edu_courses>();
		
		for(int i=0;i<j.length();i++){
			edu_courses ec=new edu_courses();
			try{
				JSONObject jo=j.getJSONObject(i);
				ec.code=jo.getString("code");
				ec.course_id=jo.getInt("course_id");
				ec.name=jo.getString("name");
				courseList.add(ec);
			}catch(JSONException ex){
				
			}
			
		}	
		return courseList;
	}
	
	public static edu_courses getCourseById(String Id, String userDeviceId){
		edu_courses e=new edu_courses();
		try{
			JSONArray j=connection.getCourseById(Id, userDeviceId);
			
			JSONObject ecJson=j.getJSONObject(2);
			
			e=edu_courses.getEduCourseFromJson(ecJson);
			
			e.comments=edu_comments.getCommentListFromJson(j.getJSONArray(0));
			e.tags=edu_tags.getTagListByJson(j.getJSONArray(1));
			
			
		}catch(JSONException ex){
			
		}
		return e;
	}
	
	@Override
	public String toString(){
		return this.name;
	}
	
	private static edu_courses getEduCourseFromJson(JSONObject jo){
		edu_courses ec=new edu_courses();
		try{
			
			ec.code=jo.getString("code");
			ec.course_id=jo.getInt("course_id");
			ec.name=jo.getString("name");
			ec.institution_id=jo.getInt("instution_id");
			ec.instructor_id=jo.getInt("instructor_id");
			ec.description=jo.getString("description");
			ec.short_description=jo.getString("short_description");
			ec.url=jo.getString("url");
			
		}catch(JSONException ex){
			
		}
		return ec;	
	}
}
